package com.douma.tree;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class _690_employee_importance {

    private Map<Integer, Employee> map = new HashMap<>();
    int res = 0;

    public int getImportance(List<Employee> employees, int id) {
        for (Employee e : employees) {
            map.put(e.id, e);
        }

        return postOrder(id);
    }

    // 递归 - 前序遍历
    public void preOrder(int id) {
        Employee emp = map.get(id);
        if (emp == null) return;

        res += emp.importance;

        // 多次递归调用
        for (int child : emp.subIds) {
            preOrder(child);
        }
    }

    // 递归 - 后序遍历
    // 计算 id 重要度之和
    public int postOrder(int id) {
        Employee emp = map.get(id);
        if (emp == null) return 0;

        // 多次递归调用
        int total = 0;
        for (int child : emp.subIds) {
            total += postOrder(child);
        }

        return total + emp.importance;
    }
}
